/*
 * This file is part of PDBEdit.
 * 
 * PDBEdit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * PDBEdit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with PDBEdit.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Creation date : 31/08/2010
 * Copyright (c) Guerfyld 2010. All rights reserved.
 * http://......
 */
package pdbfile;

/**
 * The PDBTime class wrap PilotDB Time field into java Date type
 *
 * @author guerfyld
 * @version 1.0
 */
public class PDBTime implements PDBType {
	private int intVal;

	public static final int UNDEFINED = 24 << 8;
	private static final String ERROR = "n/a";

	/**
	 * Constructor
	 *
	 * @param val	PilotDB Time field raw value
	 */
	public PDBTime(int val) {
		intVal = val;
	}

	/**
	 * Constructor
	 *
	 * @param stm	Stream to read
	 * @param pos	Start read position in stream
	 */
	public PDBTime(PDBInputStream stm, long pos) throws Exception {
		intVal = (int)stm.readValue(pos, 2);
	}

	/**
	 * Get PilotDB Time field raw value
	 *
	 * @return	PilotDB Time field raw value
	 */
	public int getValue() {
		return intVal;
	}

	/**
	 * Set time value
	 *
	 * @param date	Time value
	 */
	public void setValue(int val) {
		intVal = val;
	}

	/**
	 * Compares two PDBTime objects.
	 * The result is a negative integer if this PDBTime object precedes the argument PDBTime.
	 * The result is a positive integer if this PDBTime object follows the argument PDBTime.
	 * The result is zero if the PDBTime are equal.
	 *
	 * @param val	The PDBTime to be compared
	 * @return	Result value
	 */
	public int compareTo(PDBType val) throws IllegalArgumentException {
		if (val instanceof PDBTime) {
			if(intVal > ((PDBTime)val).intVal)
				return 1;
			else if(intVal == ((PDBTime)val).intVal)
				return 0;
			else
				return -1;
		}
		else
			throw new IllegalArgumentException();
	}

	/**
	 * Returns a string representation of the object.
	 *
	 * @return	a string representation of the object.
	 */
	public String toString() {
		return toString(intVal);
	}

	/**
	 * Returns length of raw data
	 */
	public long length() {
		return 2;
	}

	/**
	 * Clone object
	 * @return	New object
	 */
	public PDBType clone() {
		return new PDBTime(intVal);
	}

	// Format time value
	private static String toString(int val) {
		if ((val >> 8) == 24) return ERROR;

		StringBuffer sz = new StringBuffer(5);
		// Hours
		int dspVal = val >> 8;
		if (dspVal < 10) sz.append("0");
		sz.append(dspVal);
		sz.append(":");
		// Minute
		dspVal = val & 0xFF;
		if (dspVal < 10) sz.append("0");
		sz.append(dspVal);

		return sz.toString();
	}

	/**
	 * Write fields to stream
	 *
	 * &param stm	Stream to write
	 */
	public void write(PDBOutputStream stm) throws Exception {
		stm.writeValue(intVal, 2);
	}
}